"""
批量插入数据
"""
import pymysql

from utils import random_pass, password_to_sha256

conn = pymysql.connect(host='47.104.31.138', port=3306,
                       user='root', password='Luohao.618',
                       database='hrs', charset='utf8mb4')
try:
    with conn.cursor() as cursor:
        no = 1
        for _ in range(10):
            params = []
            for _ in range(100):
                password = random_pass(length=8)
                password_digest = password_to_sha256(password)
                params.append((f'user{no}', password, password_digest))
                no += 1
            # 批量插入 ---> executemany
            # 第一个参数还是SQL语句，第二个参数是一个列表，列表中的元素是元组，用来保存每条SQL语句用到的参数
            cursor.executemany(
                'insert into `tb_user` (`user_name`, `user_pass`, `user_pass2`) values (%s, %s, %s)',
                params
            )
        conn.commit()
except pymysql.MySQLError as err:
    print(err)
    conn.rollback()
finally:
    conn.close()
